﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Collections;
using System.Data.SqlClient;
using System.Data;
using System.IO;
using System.Net;
using System.Diagnostics;
using Media.Core.Data;
using Media.Core.Models;
using Media.Core.Helpers.Movies;
using Media.Core.Queries;
using Media.Core.Helpers.Main;
using Movies = Media.Core.Data.Queries.Movies;

namespace Media.Web.Controllers
{
    public class MovieController : Controller
    {
        
         //GET: /Movie/

        public ActionResult Index()
        {
            var dtMovieList = Connections.GetDataTable("Movie_Get_CompleteList");

            var MovieList = new MovieArray();

            foreach (DataRow drMovieList in dtMovieList.Rows)
            {
                MovieList.Movies.Add(new Movie
                {
                    Id = Convert.ToInt32(drMovieList["Id"].ToString()),
                    Title = drMovieList["MovieTitle"].ToString()
                });
            }

            return View(MovieList);
        }

        public ActionResult UpdateMovieList()
        {

            DirectoryInfo objDir = new DirectoryInfo(GetDirectory.Location("Movies"));
            
            System.IO.FileInfo[] fileNames = objDir.GetFiles("*.*");

            System.IO.DirectoryInfo[] dirInfos = objDir.GetDirectories("");

            for (int i = 0; i < fileNames.Count(); i++)
            {
                var file = fileNames[i].Name.ToString();

                if (Checker.CheckExtension(file) == false)
                    continue;

                var Title = Checker.RemoveExtension(file);

                try
                {
                    IMDb teste = new IMDb("Modern Family Season 1 Episode 1 pilot", true);

                    
                    
                    IMDb imdb = new IMDb(Title, true);

                    var imageSaveAndGet = ImageGrabber.GetAndSave(imdb.Poster, Title, "Movie Poster", "Movie");

                    ArrayList paramsList = new ArrayList
                    {
                        new SqlParameter("Title", imdb.Title),
                        new SqlParameter("FileName", file),
                        new SqlParameter("Plot", imdb.Plot),
                        new SqlParameter("Directors", imdb.Directors[0]),
                        new SqlParameter("Year", imdb.Year),
                        new SqlParameter("Rating", imdb.Rating),
                        new SqlParameter("SaveLocation", imageSaveAndGet),
                        new SqlParameter("Type", "Movie")
                    };

                    DataRow NewMovie = Connections.GetDataRow("Movie_Insert_NewMovies", paramsList);
                }
                catch
                {
                    ArrayList paramsList = new ArrayList
                    {
                        new SqlParameter("Title", Title),
                    };

                    DataRow NewMovie = Connections.GetDataRow("Movie_Insert_NotFound", paramsList);
                    continue;
                }
            }

            return Json("add", JsonRequestBehavior.AllowGet);
            //}

            //foreach (System.IO.FileInfo d in fileNames)
            //{
            //    if (Checker.CheckExtension(d.Name.ToString()) == false)
            //        continue;

            //    var Title = Checker.RemoveExtension(d.Name.ToString());
            //    IMDb imdb = new IMDb(Title, false);

            //    var imageSaveAndGet = ImageGrabber.GetAndSave(imdb.Poster, Title, "Movie Poster");

            //    ArrayList paramsList = new ArrayList
            //    {
            //        new SqlParameter("Title", imdb.Title),
            //        new SqlParameter("FileName", d.Name.ToString()),
            //        new SqlParameter("Plot", imdb.Plot),
            //        new SqlParameter("Directors", imdb.Directors[0]),
            //        new SqlParameter("Year", imdb.Year),
            //        new SqlParameter("Rating", imdb.Rating),
            //        new SqlParameter("SaveLocation", imageSaveAndGet),
            //        new SqlParameter("Type", "Movie")
            //    };

            //    DataRow NewMovie = Data.Connections.GetDataRow("Movie_Insert_NewMovies", paramsList);
            //}

            //return Json("add", JsonRequestBehavior.AllowGet);
        }

        public ActionResult Edit(int Id)
        {
            ArrayList paramList = new ArrayList
            {
                new SqlParameter("Id", Id)
            };

            var drMovie = Connections.GetDataRow("GetMovie", paramList);

            var Movie = new Movie
            {
                Title = drMovie["MovieTitle"].ToString(),
                Year = drMovie["ReleaseDate"].ToString(),
                Director = drMovie["Director"].ToString(),
                Quality = drMovie["Quality"].ToString(),
                Rating = (float)Convert.ToDouble(drMovie["Rating"].ToString()),
                Favourite = drMovie["Favourite"].ToString()
            };

            return View(Movie);
        }

        public ActionResult Create()
        {
            return View("Create");
        }

        public ActionResult Details(int Id)
        {
            ArrayList paramList = new ArrayList
            {
                new SqlParameter("Id", Id)
            };

            var drMovie = Connections.GetDataRow("Movie_Get_ById", paramList);

            var Movie = new Movie
            {
                Id = Convert.ToInt32(drMovie["Id"].ToString()),
                Title = drMovie["MovieTitle"].ToString(),
                Director = drMovie["Director"].ToString(),
                Year = drMovie["ReleaseDate"].ToString(),
                Quality = drMovie["Quality"].ToString(),
                Description = drMovie["Description"].ToString(),
                Rating = (float)Convert.ToDouble(drMovie["Rating"].ToString()),
                Favourite = drMovie["Favourite"].ToString(),
                image = drMovie["Image"].ToString()
            };

            var MovieDetails = Movies.GetMovieDetails(Id);

            //IMDb imdb = new IMDb(Movie.Title, false);

            return View(MovieDetails);
        }

        public ActionResult GetMovieInfo(string Title)
        {
            IMDb imdb = new IMDb(Title, false);

            return Json(imdb, JsonRequestBehavior.AllowGet);
        }

    }
}
